import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@v/Layout'
import { tokenExist, getRole } from '@/utils/user'
// import store from '@/store'
const routes = [
  {
    path: '/',
    name: 'layout',
    component: Layout,
    children: [
      {
        path: '/',
        redirect: '/dashBoard'
      },
      {
        path: '/dashBoard',
        name: '首页',
        meta: {
          nameArr: ['首页'],
          roles: '*'
        },
        component: () => import('@v/DashBoard')
      },
      {
        path: '/tradeMsg',
        name: '商业区管理',
        meta: {
          nameArr: ['商业区管理', '资产管理'],
          roles: ['admin', 'a', 'b']
        },
        component: () => import('@v/TradeMsg')
      },
      {
        path: '/towerMsg',
        name: '楼栋管理',
        meta: {
          nameArr: ['楼栋管理', '资产管理'],
          roles: ['admin', 'a', 'b']
        },
        component: () => import('@v/TowerMsg')
      },
      {
        path: '/getMoney',
        name: '收费项管理',
        meta: {
          nameArr: ['收费项管理', '收费设置'],
          roles: ['admin', 'a', 'b']
        },
        component: () => import('@v/GetmoneyMsg')
      },
      {
        path: '/contractMsg',
        name: '租赁合同管理',
        meta: {
          nameArr: ['租赁合同管理', '收费设置'],
          roles: ['admin', 'a', 'b']
        },
        component: () => import('@v/ContractMsg')
      },
      {
        path: '/setMsg',
        name: '系统管理',
        meta: {
          nameArr: ['系统管理'],
          roles: ['a', 'b']
        },
        component: () => import('@v/SetMsg')
      },
      {
        path: '/mineInfo',
        name: '个人中心',
        meta: {
          nameArr: ['个人中心'],
          roles: '*'
        },
        component: () => import('@v/MineInfo')
      }
    ]
  },
  {
    path: '/loginPage',
    meta: {
      roles: ['admin', 'a', 'b']
    },
    component: () => import('@v/LoginPage')
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@v/NotFound'),
    meta: {
      roles: '*'
    }
  },
  {
    path: '/notPower',
    component: () => import('@v/NotPower'),
    meta: {
      roles: '*'
    }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  // ...
  // 返回 false 以取消导航
  // console.log(from)
  // if (to.name === '首页') {
  //   console.log(12243546)
  //   next('/')
  // } else {
  // }
  const acessRoute = ['/loginPage']
  if (acessRoute.includes(to.path)) {
    next()
  } else {
    if (tokenExist()) {
      console.log(to.meta.roles)
      console.log(to.path)
      if (to.meta.roles === '*' || to.meta.roles.includes(getRole())) {
        next()
      } else {
        next('/notPower')
      }
    } else {
      next('/loginPage')
    }
  }
})
export default router
